/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmThongBaoChupAnh.java
 *
 * Created on May 28, 2011, 1:37:12 AM
 */

package Presentation;

import RSCall.DiaOcRSc;
import RSCall.DuongRSc;
import RSCall.HinhAnhRSc;
import RSCall.KhachHangRSc;
import RSCall.NoiDungRSc;
import RSCall.PhieuDangKyRSc;
import RSCall.PhuongRSc;
import RSCall.QuanRSc;
import RSCall.ThanhPhoRSc;
import Entities.DIAOC;
import Entities.KHACHHANG;
import Entities.NOIDUNGQC;
import Entities.PHIEUDANGKY;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.sql.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VooKa
 */
public class frmThongBaoChupAnh extends javax.swing.JInternalFrame {

    /** Creates new form frmThongBaoChupAnh */
    public frmThongBaoChupAnh() {
        initComponents();
        Creation();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabMain = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblHomNay = new javax.swing.JTable();
        lbThongBao = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblChuaChup = new javax.swing.JTable();
        lblThongBao = new javax.swing.JLabel();
        btnThongBao = new javax.swing.JButton();

        setClosable(true);
        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("frmChupAnh"));
        setPreferredSize(new java.awt.Dimension(999, 650));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        tblHomNay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblHomNay);

        lbThongBao.setText("label1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lbThongBao)
                        .addGap(0, 910, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbThongBao)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabMain.addTab("Hôm nay", jPanel1);

        tblChuaChup.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblChuaChup);

        lblThongBao.setText("jLabel1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 948, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblThongBao)
                        .addContainerGap(914, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblThongBao)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabMain.addTab("Danh Sách Địa Ốc Cần Chụp Ảnh", jPanel2);

        btnThongBao.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("warntocustomer"));
        btnThongBao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThongBaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabMain)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnThongBao)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnThongBao)
                .addGap(31, 31, 31)
                .addComponent(tabMain, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        tabMain.getAccessibleContext().setAccessibleName("Danh Sách Địa Ốc Chụp Ảnh Hôm");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.LoadDataNeed();
        this.LoadDataToDay();
    }//GEN-LAST:event_formInternalFrameOpened

    private void btnThongBaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThongBaoActionPerformed
        if (tabMain.getSelectedIndex() == 0 && tblHomNay.getSelectedRow() != -1) {
            try {
                String MaKH = tblHomNay.getValueAt(tblHomNay.getSelectedRow(), 4).toString();
                KHACHHANG k = dbKhachHang.GetRowByID(MaKH);
                ArrayList<String> listEmail = new ArrayList<String>();
                listEmail.add(k.getEmail());
                SendMail frm = new SendMail(listEmail);
                Utilities.Utilities.setFontForAll(frm, new Font("Calibri", 0, 14));
                frm.setLocation(155, 0);
                frm.setClosable(true);
                frm.show();
                this.getParent().add(frm,0);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, e.toString());
            }
        }
        if (tabMain.getSelectedIndex() == 1 && tblChuaChup.getSelectedRow() != -1) {
            try {
                String MaKH = tblChuaChup.getValueAt(tblChuaChup.getSelectedRow(), 4).toString();
                KHACHHANG k = dbKhachHang.GetRowByID(MaKH);
                ArrayList<String> listEmail = new ArrayList<String>();
                listEmail.add(k.getEmail());
                SendMail frm = new SendMail(listEmail);
                Utilities.Utilities.setFontForAll(frm, new Font("Calibri", 0, 14));
                frm.setLocation(155, 0);
                frm.setClosable(true);
                frm.show();
                this.getParent().add(frm,0);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, e.toString());
            }
        }else JOptionPane.showMessageDialog(rootPane, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mustselectGiaTien"));
    }//GEN-LAST:event_btnThongBaoActionPerformed
    

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThongBao;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbThongBao;
    private javax.swing.JLabel lblThongBao;
    private javax.swing.JTabbedPane tabMain;
    private javax.swing.JTable tblChuaChup;
    private javax.swing.JTable tblHomNay;
    // End of variables declaration//GEN-END:variables
    private RSCall.DiaOcRSc dbDiaOc;
    private RSCall.KhachHangRSc dbKhachHang;
    private RSCall.PhieuDangKyRSc dbPhieuDK;
    private RSCall.NoiDungRSc dbNoiDungQC;
    private RSCall.HinhAnhRSc dbHinhAnh;
    private RSCall.ThanhPhoRSc dbThanhPho;
    private RSCall.QuanRSc dbQuan;
    private RSCall.PhuongRSc dbPhuong;
    private RSCall.DuongRSc dbDuong;
    
    private ArrayList<DIAOC> result;
    
    public void Creation(){
        try {
            dbDiaOc = new DiaOcRSc();
            dbKhachHang = new KhachHangRSc();
            dbPhieuDK = new PhieuDangKyRSc();
            dbNoiDungQC = new NoiDungRSc();
            dbHinhAnh = new HinhAnhRSc();
            dbThanhPho = new ThanhPhoRSc();
            dbQuan = new QuanRSc();
            dbPhuong = new PhuongRSc();
            dbDuong = new DuongRSc();
            result = new ArrayList<DIAOC>();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
            Logger.getLogger(frmThongBaoChupAnh.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void LoadDataNeed(){
        try
        {
            DefaultTableModel dtm = new DefaultTableModel();
            
            dtm.addColumn("STT");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("time"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("propertyid"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("address"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("customerid"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("hoten"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("soDT"));
            dtm.addColumn("Email");
            
            ArrayList<DIAOC> listDiaOc = new ArrayList<DIAOC>();
            for(DIAOC d : dbDiaOc.mainList){
                if(d.getTrangThai().equals(frmMain.ChuaBan)){
                    NOIDUNGQC noiDung = dbNoiDungQC.GetRowByID(dbDiaOc.GetMaND_From_MaDiaOc(d.getMaDiaOc()));
                    if (!dbHinhAnh.IsMaND_HinhAnhExist(noiDung.getMaND())) {
                        listDiaOc.add(d);
                    }
                }
            }
            if (listDiaOc.size() > 0) {
                int n = 0;
                for(DIAOC diaOc : listDiaOc){
                    PHIEUDANGKY p = dbPhieuDK.GetPhieuDKByMaDiaOc(diaOc.getMaDiaOc());
                    KHACHHANG k = dbKhachHang.GetRowByID(p.getMaKH());
                    Vector v = new Vector();
                    v.add(++n);
                    v.add(p.getThoiGianHenChupAnh());
                    v.add(diaOc.getMaDiaOc());
                    String tenThanhPho = dbThanhPho.GetRowByID(diaOc.getDiaChiDiaOc().getMaThanhPho()).getTenThanhPho();
                    String tenQuan = dbQuan.GetRowByID(diaOc.getDiaChiDiaOc().getMaQuan()).getTenQuan();
                    String tenPhuong = dbPhuong.GetRowByID(diaOc.getDiaChiDiaOc().getMaPhuong()).getTenPhuong();
                    String tenDuong = dbDuong.GetRowByID(diaOc.getDiaChiDiaOc().getMaDuong()).getTenDuong();
                    String diaChiCT = diaOc.getDiaChiDiaOc().getDiaChiCT().trim();
                    String diaChi = diaChiCT + " " + tenDuong + " " + tenPhuong + " " + tenQuan + " " + tenThanhPho;
                    v.add(diaChi);
                    v.add(k.getMaKH());
                    v.add(k.getHoTen());
                    v.add(k.getSoDT());
                    v.add(k.getEmail());
                    dtm.addRow(v);
                }
                
                this.tblChuaChup.setModel(dtm);
                this.lblThongBao.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("co") 
                        + listDiaOc.size() 
                        + java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("diaoccanchupanh"));
                this.tblChuaChup.getColumnModel().getColumn(0).setPreferredWidth(10);
                this.tblChuaChup.getColumnModel().getColumn(1).setPreferredWidth(10);
                this.tblChuaChup.getColumnModel().getColumn(2).setPreferredWidth(10);
                this.tblChuaChup.getColumnModel().getColumn(3).setPreferredWidth(180);
                this.tblChuaChup.getColumnModel().getColumn(4).setPreferredWidth(10);
                this.tblChuaChup.getColumnModel().getColumn(5).setPreferredWidth(10);
                result = (ArrayList<DIAOC>)listDiaOc.clone();
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
    
    private void LoadDataToDay(){
        try
        {
            DefaultTableModel dtm = new DefaultTableModel();
            
            dtm.addColumn("STT");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("time"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("propertyid"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("address"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("customerid"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("hoten"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("soDT"));
            dtm.addColumn("Email");
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date nowTime = Date.valueOf(sdf.format(cal.getTime()));
            if (result.size() > 0) {
                int n = 0;
                for(DIAOC diaOc : result){                    
                    PHIEUDANGKY p = dbPhieuDK.GetPhieuDKByMaDiaOc(diaOc.getMaDiaOc());
                    if (p.getThoiGianHenChupAnh().compareTo(nowTime) == 0) {
                        KHACHHANG k = dbKhachHang.GetRowByID(p.getMaKH());
                        Vector v = new Vector();
                        v.add(++n);
                        v.add(p.getThoiGianHenChupAnh());
                        v.add(diaOc.getMaDiaOc());
                        String tenThanhPho = dbThanhPho.GetRowByID(diaOc.getDiaChiDiaOc().getMaThanhPho()).getTenThanhPho();
                        String tenQuan = dbQuan.GetRowByID(diaOc.getDiaChiDiaOc().getMaQuan()).getTenQuan();
                        String tenPhuong = dbPhuong.GetRowByID(diaOc.getDiaChiDiaOc().getMaPhuong()).getTenPhuong();
                        String tenDuong = dbDuong.GetRowByID(diaOc.getDiaChiDiaOc().getMaDuong()).getTenDuong();
                        String diaChiCT = diaOc.getDiaChiDiaOc().getDiaChiCT().trim();
                        String diaChi = diaChiCT + " " + tenDuong + " " + tenPhuong + " " + tenQuan + " " + tenThanhPho;
                        v.add(diaChi);
                        v.add(k.getMaKH());
                        v.add(k.getHoTen());
                        v.add(k.getSoDT());
                        v.add(k.getEmail());
                        dtm.addRow(v);
                    }
                }
                this.tblHomNay.setModel(dtm);
                this.lbThongBao.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("co")
                        + n + 
                        java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("diaoccanchupanhhomnay"));
                this.tblHomNay.getColumnModel().getColumn(0).setPreferredWidth(10);
                this.tblHomNay.getColumnModel().getColumn(1).setPreferredWidth(10);
                this.tblHomNay.getColumnModel().getColumn(2).setPreferredWidth(10);
                this.tblHomNay.getColumnModel().getColumn(3).setPreferredWidth(180);
                this.tblHomNay.getColumnModel().getColumn(4).setPreferredWidth(10);
                this.tblHomNay.getColumnModel().getColumn(5).setPreferredWidth(10);
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
}
